﻿using EECMISWS;
using HZWebService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZWebService
{
    public class WebInterface
    {
        public bool Enabled
        {
            get
            {
                return _option.Enabled;
            }
        }

        private HzClientOption _option = new HzClientOption();

        public WebInterface()
        {
            LoadSettings();
        }

        private void LoadSettings()
        {
            if (!System.IO.File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
        }

        public bool UploadPhoto(string idCardNo,string phtotName,out HzPhotoResponeArg response,out string msg)
        {
            response = new HzPhotoResponeArg();
            msg = "";
            try
            {
                if (string.IsNullOrEmpty(_option.URL))
                {
                    msg = "服务地址为空";
                    return false;
                }

                if (string.IsNullOrEmpty(_option.User))
                {
                    msg = "用户名为空";
                    return false;
                }

                if (string.IsNullOrEmpty(_option.GetPassword()))
                {
                    msg = "密码为空";
                    return false;
                }

                if (string.IsNullOrEmpty(idCardNo))
                {
                    msg = "身份证号码为空";
                    return false;
                }

                string base64string = Data2Json.FileContent2Base64(phtotName);
                if (string.IsNullOrEmpty(base64string))
                {
                    msg = "照片文件不存在";
                    return false;
                }

                FileInfo fileInfo = new FileInfo
                {
                    name = idCardNo+".jpg",
                    base64 = base64string
                };

                List<FileInfo> fileInfos = new List<FileInfo>();
                fileInfos.Add(fileInfo);

                HzPhotoRequestArg requestArg = new HzPhotoRequestArg
                {
                    user = _option.User,
                    passwd = _option.GetPassword(),
                    templateFile = 3,
                    fileInfos= fileInfos
                };

                string res = HttpHelper.Post(_option.URL+ "openBatchCheck", JsonHandler.ConvertToJsonString(requestArg));
                if (!string.IsNullOrEmpty(res))
                {
                    response = JsonHandler.ConvertToJsonObject<HzPhotoResponeArg>(res);
                    return true;
                }
                else
                {
                    msg = "范围内容为空";
                    return false;
                }                
            }
            catch(Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
    }
}
